//
//  WmcDefine.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/2.
//

import UIKit
import YPBedrock

// MARK: - 设备基础尺寸

/// 状态栏高度
var Wmc_statusBarHeight: CGFloat {
    if #available(iOS 13.0, *),
       let statusManager = YPApp.KeyWindow?.windowScene?.statusBarManager
    {
        return statusManager.statusBarFrame.size.height
    } else {
        return UIApplication.shared.statusBarFrame.size.height
    }
}

/// 屏幕宽度
var Wmc_screenWidth: CGFloat {
    return Wmc_screenSize.width
}

/// 屏幕高度
var Wmc_screenHeight: CGFloat {
    return Wmc_screenSize.height
}

/// 屏幕大小
var Wmc_screenSize: CGSize {
    return Wmc_screen.size
}

/// 屏幕坐标
var Wmc_screen: CGRect {
    return UIScreen.main.bounds
}

/// 顶部安全区
var Wmc_safeAreaTop: CGFloat {
    return Wmc_safeAreaInsets.top
}

/// 左侧安全区
var Wmc_safeAreaLeft: CGFloat {
    return Wmc_safeAreaInsets.left
}

/// 底部安全区
var Wmc_safeAreaBottom: CGFloat {
    return Wmc_safeAreaInsets.bottom
}

/// 右侧安全区
var Wmc_safeAreaRight: CGFloat {
    return Wmc_safeAreaInsets.right
}

/// 安全区边距
var Wmc_safeAreaInsets: UIEdgeInsets {
    if #available(iOS 11.0, *) {
        if let inset = YPApp.KeyWindow?.rootViewController?.view.safeAreaInsets {
            return inset
        }
    }
    return .zero
}

// MARK: - 设计规范尺寸

/// 参考宽度
let Wmc_standardWidth: CGFloat = 375
/// 参考高度
let Wmc_standardHeight: CGFloat = 812
/// 内容边距
let Wmc_standardEdge: UIEdgeInsets = .init(top: 12, left: 16, bottom: 12, right: 16)
/// 动画默认时间
let Wmc_animationDuration: CGFloat = 0.4

// MARK: - 统一动画

/// 统一弹性动画
func Wmc_SpringAnimation(withDuration: CGFloat = Wmc_animationDuration,
                         block: @escaping YPBlock.Empty,
                         completion: YPBlock.Empty? = nil)
{
    UIView.animate(withDuration: withDuration,
                   delay: 0,
                   usingSpringWithDamping: 0.6,
                   initialSpringVelocity: 0.01,
                   options: .curveEaseInOut) {
        block()
    } completion: { _ in
        completion?()
    }
}

func Wmc_ViewSpringAnimation(_ view: UIView,
                             isShow: Bool,
                             block: YPBlock.Empty? = nil,
                             completion: YPBlock.Empty? = nil) {
    if isShow { // 隐藏，需要显示
        guard view.isHidden else { return }
        view.isHidden = false
        view.transform = CGAffineTransform.YP.CreateScale(0)
        view.alpha = 0
        Wmc_SpringAnimation {
            view.transform = CGAffineTransform.YP.CreateScale(1)
            view.alpha = 1
            block?()
        }completion: {
            completion?()
        }
    } else { // 显示，需要隐藏
        guard !view.isHidden else { return }
        view.alpha = 1
        Wmc_SpringAnimation {
            view.alpha = 0
            block?()
        } completion: {
            view.isHidden = true
            completion?()
        }
    }
}
